package com.wu.base;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.wu.enums.SystemEnum;
import com.wu.exception.EnumInterface;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResultUtils {
    public static ApiResponse setError(EnumInterface baseEnum) {
        return setError(null, baseEnum);
    }

    public static ApiResponse setError(ApiResponse response, EnumInterface baseEnum) {
        if (response == null) {
            response = new ApiResponse();
        }
        response.setApiResponse(baseEnum);
        return response;
    }

    public static ApiResponse setError(String code, String message) {
        ApiResponse response = new ApiResponse();
        response.setMsg(message);
        response.setCode(code);
        response.setData("");
        return response;
    }

    public static ApiResponse setError(String mes) {
        ApiResponse response = new ApiResponse();
        response.setMsg(mes);
        response.setCode(SystemEnum.FAILED.getCode());
        response.setData("");
        return response;
    }

    public static ApiResponse setError(EnumInterface baseEnum, String mes) {
        ApiResponse response = new ApiResponse();
        response.setMsg(mes);
        response.setCode(baseEnum.getCode().toString());
        response.setData("");
        return response;
    }

    public static ApiResponse setOk() {
        ApiResponse response = new ApiResponse();
        response.setApiResponse(SystemEnum.SUCCESS);
        response.setData("");
        return setOk(response);
    }

    public static ApiResponse setOk(ApiResponse response) {
        if (response == null) {
            response = new ApiResponse();
            response.setData("");
        }
        response.setApiResponse(SystemEnum.SUCCESS);
        return response;
    }

    public static ApiResponse setOk(List<?> objectList) {
        ApiResponse response = new ApiResponse();
        response.setApiResponse(SystemEnum.SUCCESS);
        Map<String, Object> map = new HashMap<>();
        map.put("list", objectList);
        response.setData(map);
        return response;
    }

    public static ApiResponse setOk(String data) {
        ApiResponse response = new ApiResponse();
        response.setApiResponse(SystemEnum.SUCCESS);
        response.setData(data);
        return response;
    }

    public static ApiResponse setOk(Object data, String mes) {
        ApiResponse response = new ApiResponse();
        response.setApiResponse(SystemEnum.SUCCESS);
        response.setMsg(mes);
        response.setData(data);
        return response;
    }

    public static ApiResponse setOk(Object data) {
        return setOk(null, data);
    }

    public static ApiResponse setOk(ApiResponse response, Object data) {
        if (response == null) {
            response = new ApiResponse();
        }
        response.setApiResponse(SystemEnum.SUCCESS);
        response.setData(data);
        return response;
    }

    public static ApiResponse setOk(Object data, Integer currentPage, Integer pageSize, Long total, Integer pages) {
        ApiResponse response = new ApiResponse();
        response.setApiResponse(SystemEnum.SUCCESS);
        Map<String, Object> map = new HashMap<>();
        map.put("list", data);
        map.put("total", total);
        map.put("pageSize", pageSize);
        map.put("pages", pages);
        map.put("currentPage", currentPage);
        response.setData(map);
        return response;
    }

    public static ApiResponse setOk(PageInfo pageInfo) {
        ApiResponse response = new ApiResponse();
        response.setApiResponse(SystemEnum.SUCCESS);
        PageInfoData result = new PageInfoData();
        result.setList(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        result.setPageSize(pageInfo.getPageSize());
        result.setPages(pageInfo.getPages());
        result.setCurrentPage(pageInfo.getPageNum());

        response.setData(result);
        return response;
    }

    public static ApiResponse setOk(Iterable<?> data, PageInfo pageInfo) {
        ApiResponse response = new ApiResponse();
        response.setApiResponse(SystemEnum.SUCCESS);
        PageInfoData result = new PageInfoData();
        result.setList(Lists.newArrayList(data));
        result.setTotal(pageInfo.getTotal());
        result.setPageSize(pageInfo.getPageSize());
        result.setPages(pageInfo.getPages());
        result.setCurrentPage(pageInfo.getPageNum());
        response.setData(result);
        return response;
    }

    public static ApiResponse setOkOrError(Integer flag) {
        ApiResponse response = new ApiResponse();
        response.setData("");
        if (flag > 0) {
            response.setApiResponse(SystemEnum.SUCCESS);
        } else {
            response.setApiResponse(SystemEnum.FAILED);
        }
        return response;
    }

    public static ApiResponse setOk(Map<?, ?> map) {
        ApiResponse response = new ApiResponse();
        response.setApiResponse(SystemEnum.SUCCESS);
        response.setData(map);
        return setOk(response);
    }
}
